<?php

class User_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function saveUser($data) {

        $this->db->set($data);
        $this->db->insert('url_users');
        $id = $this->db->insert_id();
        $this->result = $this->db->insert_id();
        return $this->result;
    }

    function getUser() {
        $this->db->select('*');
        $this->db->from('url_users');
        $this->db->order_by("id", "asc");
        $query = $this->db->get();
        return $query->result();
    }
    function getImage() {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;
        $this->db->select('image');
         $this->db->where('id', $id);
        $this->db->from('url_users');
       $query = $this->db->get();
        return $query->result();
      //   echo $this->db->last_query();
    }

    function DeleteUser($id) {

        $id = $_GET['id'];
        $this->db->delete('url_users', array('id' => $id));
        $location = 'location:' . base_url() . "user?msg=SuccessFully Deleted id $id";
        header($location);
    }

    function editUser() {
        $id = $_GET['id'];
        $this->db->select('*');
        $this->db->where('id', $id);
        $query = $this->db->get('url_users');
        return $query->result();
    }
    
    function editProfile() {
        $id = $_GET['id'];
        $this->db->select('*');
        $this->db->where('id', $id);
        $query = $this->db->get('url_users');
        return $query->result();
    }

    function profile() {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;
        $this->db->select('*');
        $this->db->where('id', $id);
        $query = $this->db->get('url_users');
        return $query->result();
    }

    function updateUser($data) {
        $id = $this->input->post('id');
        $data = array(
            'name' => $data['name'],
            //'username' => $data['username'],
            'email' => $data['email'],
            'password' => $data['password'],
                //  'create_time' => $data['Y-m-d'],
        );
        $this->db->where('id', $id);
        $this->db->update('url_users', $data);
        return $this->db->affected_rows();
    }
        function Editpro($data) {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;
        //print_r($data); die;
        $this->db->where('id', $id);
        $this->db->update('url_users', $data);
        return $this->db->affected_rows();
    }

    function check_user($name) {
        $query = $this->db->get_where('url_users', array('username' => $name));
        $uresult = $query->result();
        return $uresult;
    }

    function setStatus($post) {
        $data = array(
            'status' => $post['status']);

        $this->db->where('id', $post['id']);
        $this->db->update('url_users', $data);
        //  echo $this->db->last_query();

        return true;
    }

    function changePassword($data) {
        $type = $this->session->all_userdata();
        $type = $type['0'];
        $id = $type->id;
        $data = array('password' => $data['password'],);
        //print_r($data); die;
        $this->db->where('id', $id);
        $this->db->update('url_users', $data);

        $this->session->set_flashdata('msg', 'Your Password Successfully Changed');
        redirect(base_url() . 'login/logout');
    }

    function check_email($str) {
        $this->db->select('email');
        $this->db->from('url_users');
        $this->db->where('email', $str);
        $count = $this->db->count_all_results();

        if ($count > 0) {
            return true;
        } else {
            return false;
        }
    }

    public function yoururl() {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;
        $this->db->where('user_id', $id);
        $this->db->from('short_url');
        return $this->db->count_all_results();
    }

    function do_upload() {
        $type = $this->session->all_userdata();
        $type = $type['0'];
        $id = $type->id;
        echo $fileExt = end(explode('.',$_FILES['image']['name']));
        
        $image = $type->username.".jpg";
        $this->db->where('id', $id);
        $this->db->set('image', $image);
        $this->db->update('url_users');
     //   $this->db->where('id', $id);
      //   echo $this->db->last_query(); die;
        $query = $this->db->get('url_users');
        return $query->result();
    }
    function editown(){
        $type = $this->session->all_userdata();
        $type = $type['0'];
        $id = $type->id;
        $this->db->where('id', $id);
        $query=$this->db->get('url_users');
        return $query->result();
    }

}

?>
